const express = require("express");
const router = express.Router();
const mysql = require("mysql");
const url = require("../user/path");
const fs = require("fs");
const env = {
  host: "111.231.22.97",
  user: "wangs-web",
  password: "HsTjw82zF7f6fjWY",
  database: "wangs-web", //数据库名字
};

const filters = (e) => {
  let objs = {};
  for (let keys in e) {
    if (e[keys] != "" && e[keys] != undefined && e[keys] != null) {
      objs[keys] = e[keys];
    }
  }
  return Object.keys(objs);
};

//查询代办
const searchHandle = (e, req, res) => {
  if (e.dept == "3" && e.locat == "2") {
    let pools = mysql.createPool(env);
    let sql = "select * from noticelist where status = ?";
    pools.getConnection(function (err, connection) {
      if (err) throw err;
      connection.query(sql, [0], (err, result) => {
        if (err) {
          res.status(500);
          res.send({
            code: 500,
            msg: "error",
          });
        } else {
          console.log(result, "222");
          let resArr = result.reverse();
          resArr.forEach((ele, ind) => {
            ele.index = ind;
          });
          res.send({
            code: 200,
            msg: "success",
            data: {
              data: resArr,
            },
          });
        }
      });
    });
  }
  //暂时
  else {
    res.send({
      code: 200,
      msg: "success",
      data: {
        data: [],
      },
    });
  }
};
//查询我的事项
const searchMine = (req, res) => {
  let pools = mysql.createPool(env);
  pools.getConnection(function (err, connection) {
    if (err) throw err;
    let sql = "select * from noticelist where status=?";
    connection.query(sql, [1], (err, result) => {
      if (err) {
        res.status(500);
        res.send({
          code: 500,
          msg: "error",
        });
      }
      if (result.length >= 0) {
        let resArr = result.reverse();
        res.send({
          code: 200,
          msg: "success",
          data: {
            data: resArr,
          },
        });
      }
    });
  });
};

//任务列表查询下属
const searchUp = (req, res) => {
  let sql = "select * from userlist where dept=? and locat=?";
  let pools = mysql.createPool(env);
  pools.getConnection(function (err, connection) {
    if (err) throw err;
    connection.query(sql, [req.query.dept, 3], (err, result) => {
      if (err) {
        res.status(500);
        res.send({
          code: 500,
          msg: "error",
        });
      }
      let resArr = [];
      result.forEach((ele) => {
        let obj = {
          label: ele.name,
          value: ele.username,
        };
        resArr.push(obj);
      });
      res.send({
        code: 200,
        msg: "success",
        data: {
          data: resArr,
        },
      });
    });
  });
};

//十位随机
const indexs = () => {
  let str = "";
  for (let i = 0; i < 10; i++) {
    let ind = Math.ceil(Math.random() * 10);
    str += ind;
  }
  return str;
};

//新增任务列表
const postTask = (req, res) => {
  let id = indexs();
  const {
    taskName,
    taskStartTime,
    taskEndTime,
    taskForm,
    taskTo,
    taskDetail,
    dept,
  } = req.body;
  if (req.body.type == "add") {
    console.log("新增");
    let sql = "insert into tasklist values(?,?,?,?,?,?,?,?,?)";
    let pools = mysql.createPool(env);
    pools.getConnection(function (err, connection) {
      if (err) throw err;
      connection.query(
        sql,
        [
          id,
          taskName,
          taskStartTime,
          taskEndTime,
          taskForm,
          taskTo,
          taskDetail,
          1,
          dept,
        ],
        (err, result) => {
          if (err) {
            res.status(500);
            res.send({
              code: 500,
              msg: "error",
            });
          } else {
            if (result.affectedRows == 1) {
              res.send({
                code: 200,
                msg: "success",
              });
            } else {
              res.send({
                code: 500,
                msg: "error",
              });
            }
          }
        }
      );
    });
  } else {
  }
};

//查询通知详情
const getNotice = (req, res) => {
  let sql = "select id from noticelist where title=?";
  let pools = mysql.createPool(env);
  pools.getConnection(function (err, connection) {
    if (err) throw err;
    connection.query(sql, [req.query.title], (err, result) => {
      if (err) {
        res.status(500);
        res.send({
          code: 500,
          msg: "error",
        });
      } else {
        console.log(result);
        let urls = url.paths + +result[0].id + ".txt";
        fs.readFile(urls, "utf-8", (err, dataStr) => {
          if (err) throw err;
          res.send({
            code: 200,
            msg: "success",
            data: {
              data: dataStr,
            },
          });
        });
      }
    });
  });
};

//查询任务
const taskFilter = (arr) => {
  let tarArr = ["taskName", "taskStartTime", "taskStartTime"];
  let newArr = arr.filter((res) => {
    return !tarArr.includes(res);
  });
  return newArr;
};

const getTask = (req, res) => {
  let arr = filters(req.query);
  let newArr = taskFilter(arr);
  if ((req.type = "myTask")) {
    console.log("首页查询");
  } else {
    if (arr.length == 3 && newArr.length == 3) {
      console.log("主管查部门");
    } else if (arr.length == 4 && newArr.length == 4) {
      console.log("查自己的");
    } else {
      console.log("条件查询");
    }
  }
};

router.get("/pendingProcessing", (req, res) => {
  searchHandle(req.query, req, res);
});

router.get("/mineBusiness", (req, res) => {
  searchMine(req, res);
});

router.get("/searchUp", (req, res) => {
  searchUp(req, res);
});

router.post("/postTask", (req, res) => {
  postTask(req, res);
});

router.get("/getNotice", (req, res) => {
  getNotice(req, res);
});

router.get("/getTask", (req, res) => {
  getTask(req, res);
});

module.exports = router;
